<template>
  <div class="dialog">
    <el-dialog :title="'通知配置' + titleText" :visible.sync="showDialog" width="55%" :close-on-click-modal="false" @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="通知事件" prop="noticeEvent" >
          <el-select class="w100p" filterable clearable v-model="formData.noticeEvent" >
            <el-option v-for="(item, index) in noticeEvent" :key="index" :label="item.value" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="通知人" prop="noticeStaffIdList" >
          <el-select class="w100p" filterable clearable v-model="formData.noticeStaffIdList" multiple  @change="staffChange">
            <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName" :value="item.staffId" />
          </el-select>
        </el-form-item>
        <el-form-item label="通知方式" prop="noticeTypeList" >
          <el-select class="w100p" filterable clearable v-model="formData.noticeTypeList" multiple >
            <el-option v-for="(item, index) in noticeType" :key="index" :label="item.value" :value="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import NoticeConfigApi from "@/api/basic-info-manage/notice-method-config";
import StaffApi from "@/api/basic-info-manage/staff";
import EnumApi from "@/api/enum";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: "",
    },
    nowChanceStau: {
      type: String,
      default: "",
    },
    show: {
      type: Boolean,
      default: false,
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      },
    },
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      formData: {},
      rules: {
        noticeEvent: [
          { required: true, message: "请选择通知事件", trigger: ["blur"] },
        ],
        noticeStaffIdList: [
          { required: true, message: "请选择通知人", trigger: ["blur"] },
        ],
        noticeTypeList: [
          { required: true, message: "请选择通知方式", trigger: ["blur"] },
        ],
      },
      noticeType: [],
      noticeEvent: [],
      staffList: [],
      showDialog: this.show,
    };
  },
  computed: {},
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === "add") {
        let staffName = sessionStorage.getItem("staffName");
        let staffId = sessionStorage.getItem("staffId");
        let userList = this.staffList.filter((item) => {
          return Number(item.staffId) === Number(staffId);
        });
        this.formData = {
          noticeStaffIdList:[
            staffId=userList.length == 0 ? this.staffList[0].staffId : Number(staffId),
          ]
        };
        console.log('this.formData',this.formData);
      }
      if (v === "edit") {
        this.formData = this.editObject;
      }
    },
  },
  created() {},
  mounted() {
    this.queryEnum();
    this.queryStaff();
  },
  methods: {
    queryEnum() {
      EnumApi.list({ enumType: "NoticeType" }).then((res) => {
        this.noticeType = res.data;
      });
      EnumApi.list({ enumType: "NoticeEvent" }).then((res) => {
        this.noticeEvent = res.data;
      });
    },
    queryStaff() {
      StaffApi.queryByCondition().then((res) => {
        this.staffList = res.data;
      });
    },
    staffChange(e) {
      console.log(e);
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            NoticeConfigApi.edit(this.formData).then((res) => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            NoticeConfigApi.add(this.formData).then((res) => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped></style>
